package com.bnz.service.impl;

import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.service.StudentService;
import com.bnz.util.MybatisUtil;
import com.bnz.vo.StudentVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.dialect.rowbounds.MySqlRowBoundsDialect;
import org.apache.ibatis.session.SqlSession;


import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
public class StudentServiceImpl implements StudentService {
	private StudentMapper studentMapper;
	private ClassesMapper classesMapper;
	/**
	 * 1. 显示所有学生
	 * @return
	 */
	@Override
	public List<Student> findAll() {
		try(SqlSession session = MybatisUtil.getSqlSession()){
			//1.1 得到mapper
			studentMapper = session.getMapper(StudentMapper.class);
			//1.2 得到所有学生
			List<Student> students = studentMapper.findAll();
			return students;
		}
	}

	/**
	 * 2. 分页查询
	 * @param page
	 * @param pagesize
	 * @return
	 */
	@Override
	public PageResult<Student> findByPage(int page, int pagesize) {
		try(SqlSession session = MybatisUtil.getSqlSession()){
			//2.1 得到mapper
			studentMapper = session.getMapper(StudentMapper.class);
			classesMapper = session.getMapper(ClassesMapper.class);
			//2.2 开始分页
			PageHelper.startPage(page,pagesize);
			//2.3 得到所有学生
			List<Student> students = studentMapper.findAll();
			//2.4 查询学生关联的班级
			for (Student student : students) {
				Classes classes = classesMapper.findClassesByCid(student.getCid());
				student.setCname(classes.getCname());
			}
			//2.4 得到PageInfo对象
			PageInfo<Student> pageInfo = new PageInfo<>(students);
			//2.5 返回
			return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
		}
	}

	/**
	 * 3. 分页查询
	 * @param page
	 * @param pagesize
	 * @return
	 */
	@Override
	public PageInfo<Student> findByPage2(int page, int pagesize) {
		try(SqlSession session = MybatisUtil.getSqlSession()){
			//3.1 得到mapper
			studentMapper = session.getMapper(StudentMapper.class);
			//3.2 开始分页
			PageHelper.startPage(page,pagesize);
			//3.3 开始查询
			List<Student> students = studentMapper.findAll();
			//3.4 将上面的集合转换为PageInfo
			return new PageInfo<>(students);
		}

	}

	//4. 条件查询带分页
	@Override
	public PageResult<Student> search(int page, int pagesize, StudentVo studentVo) {
		//4.1 开始分页
		PageHelper.startPage(page,pagesize);
		//4.2 开始查询
		try(SqlSession session = MybatisUtil.getSqlSession()){
			//4.3 得到mapper
			studentMapper = session.getMapper(StudentMapper.class);
			classesMapper = session.getMapper(ClassesMapper.class);
			//4.4 开始查询
			List<Student> students = studentMapper.search(studentVo);
			for (Student student : students) {
				student.setCname(classesMapper.findClassesByCid(student.getCid()).getCname());
			}
			//4.5 包装成pageInfo对象
			PageInfo<Student> pageInfo = new PageInfo<>(students);
			//4.6 返回
			return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
		}

	}

	//5. 添加学生
	@Override
	public void add(Student student) {
		try(SqlSession session = MybatisUtil.getSqlSession()){
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.add(student);
		}
	}
}
